package com.ccExcelUtil.registry;

import com.ccExcelUtil.convertObject.ConvertObjectService;
import com.ccExcelUtil.service.ValidatedRelatedFieldService;
import com.ccExcelUtil.service.ValidatedRelatedSheetService;
import com.ccExcelUtil.validator.ValidatorUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务注册管理器，用于替代Spring容器的服务发现机制
 * 
 * @author xiaozhao
 */
public class ServiceRegistry {

    private static final List<ValidatedRelatedFieldService<?>> fieldServices = new ArrayList<>();
    private static final List<ValidatedRelatedSheetService<?>> sheetServices = new ArrayList<>();
    private static final List<ConvertObjectService<?, ?>> convertServices = new ArrayList<>();

    /**
     * 注册字段校验服务
     *
     * @param service 字段校验服务实现
     */
    public static void registerFieldService(ValidatedRelatedFieldService<?> service) {
        fieldServices.add(service);
        ValidatorUtils.registerFieldService(service);
    }

    /**
     * 注册Sheet校验服务
     *
     * @param service Sheet校验服务实现
     */
    public static void registerSheetService(ValidatedRelatedSheetService<?> service) {
        sheetServices.add(service);
        ValidatorUtils.registerSheetService(service);
    }

    /**
     * 注册对象转换服务
     *
     * @param service 对象转换服务实现
     */
    public static void registerConvertService(ConvertObjectService<?, ?> service) {
        convertServices.add(service);
        ValidatorUtils.registerConvertService(service);
    }

    /**
     * 批量注册字段校验服务
     *
     * @param services 字段校验服务实现列表
     */
    public static void registerFieldServices(List<ValidatedRelatedFieldService<?>> services) {
        for (ValidatedRelatedFieldService<?> service : services) {
            registerFieldService(service);
        }
    }

    /**
     * 批量注册Sheet校验服务
     *
     * @param services Sheet校验服务实现列表
     */
    public static void registerSheetServices(List<ValidatedRelatedSheetService<?>> services) {
        for (ValidatedRelatedSheetService<?> service : services) {
            registerSheetService(service);
        }
    }

    /**
     * 批量注册对象转换服务
     *
     * @param services 对象转换服务实现列表
     */
    public static void registerConvertServices(List<ConvertObjectService<?, ?>> services) {
        for (ConvertObjectService<?, ?> service : services) {
            registerConvertService(service);
        }
    }

    /**
     * 获取已注册的字段校验服务
     */
    public static List<ValidatedRelatedFieldService<?>> getFieldServices() {
        return new ArrayList<>(fieldServices);
    }

    /**
     * 获取已注册的Sheet校验服务
     */
    public static List<ValidatedRelatedSheetService<?>> getSheetServices() {
        return new ArrayList<>(sheetServices);
    }

    /**
     * 获取已注册的对象转换服务
     */
    public static List<ConvertObjectService<?, ?>> getConvertServices() {
        return new ArrayList<>(convertServices);
    }

    /**
     * 清空所有注册的服务
     */
    public static void clearAll() {
        fieldServices.clear();
        sheetServices.clear();
        convertServices.clear();
        ValidatorUtils.VALIDATED_RELATED_FIELD_SERVICE_MAP.clear();
        ValidatorUtils.VALIDATED_RELATED_SHEET_SERVICE_MAP.clear();
        ValidatorUtils.CONVERT_OBJECT_SERVICE_MAP.clear();
    }

    /**
     * 获取泛型类型名称
     */
    private static String getTypeName(Object o) {
        Type[] genericInterfaces = o.getClass().getGenericInterfaces();
        ParameterizedType genericInterface = (ParameterizedType) genericInterfaces[0];
        Type[] actualTypeArguments = genericInterface.getActualTypeArguments();
        return actualTypeArguments[0].getTypeName();
    }
}
